<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>新增企业收费</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../js/layui/css/layui.css" media="all">
</head>
<body>
<div style="width:80%;height:700px;margin:15px auto;overflow:auto;" id="aaa">
    <form class="layui-form" action="">
        <input type="hidden" id="qyID" name="qyID" class="input-text">
        <div class="layui-form-item">
            <label class="layui-form-label">缴费企业</label>
            <!--<div class="layui-input-block">-->
                <!--<input type="text" id="namebh" name="namebh" lay-verify="required" placeholder="请输入缴费公司名称" autocomplete="off" class="layui-input">-->
            <!--</div>-->
            <div class="layui-input-inline">
                <select name="city" lay-verify="" lay-filter="wuye" id="wuye" lay-search>
                    <option value="">选择物业</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="city" lay-verify="" lay-filter="qiye" id="qiye" lay-search>
                    <option value="">选择企业</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item" style="display: none" id="lalala">
            <label class="layui-form-label">到期时间</label>
            <div class="layui-input-block">
                <input type="text" id="qyDqtime" disabled autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">金额</label>
            <div class="layui-input-block">
                <input type="text" name="money" id="money" lay-verify="required|double" placeholder="请输入金额" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!--<div class="layui-form-item" style="">-->
            <!--<label class="layui-form-label">缴费时间</label>-->
            <!--<div class="layui-input-inline">-->
                <!--<input type="text" name="jfTime" id="jftime" lay-verify="required" autocomplete="off" placeholder="请输入缴费时间" class="layui-input">-->
            <!--</div>-->
            <!--&lt;!&ndash;<div class="layui-form-mid layui-word-aux">辅助文字</div>&ndash;&gt;-->
        <!--</div>-->
        <div class="layui-form-inline">
            <label class="layui-form-label">时间段</label>
            <div class="layui-input-inline">
                <input type="text" id="start_time" name="start_time" lay-verify="required" autocomplete="off" placeholder="请输入开始时间" class="layui-input">
            </div>
            <div style="display: inline;">至</div>
            <div class="layui-input-inline">
                <input type="text" id="end_time" name="end_time" lay-verify="required" autocomplete="off" placeholder="请输入结束时间" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item" style="margin-top: 20px">
            <div class="layui-input-block">
                <button class="layui-btn" id="addWuye" lay-submit lay-filter="addWuye">立即收费</button>
                <button type="reset" id="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="../../js/layui/layui.js"></script>
<script src="../../js/layui/layui.all.js"></script>
<script src="../../js/token.js"></script>
<script src="../../js/config.js"></script>

<script>

    layui.use(['form' , 'jquery'], function(){
        var form = layui.form;
        var $ = layui.jquery;
        var layer = layui.layer;

        //自定义验证规则  检测是否为数字
        form.verify({
            double: function(value){
                if(/^\d+$/.test(value)==false && /^\d+\.\d+$/.test(value)==false)
                {
                    return '你输入的不是数字';
                }
            }
        });

        //添加操作
        form.on('submit(addWuye)',function(data){
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            var qyID = $("#qiye").find("option:selected").val();
            if(qyID==null || qyID==""){
                layer.msg("请先选择缴费企业！");
                return false;
            }
            if ($("#money").val().length > 20) {
                layer.msg("金额最大长度不能超过20位！");
                return false;
            }
            if($("#money").val()==0){
                layer.msg("金额不能为0！");
                return false;
            }
            if(start_time > end_time){
                layer.msg("请输入正确时间");
                return false;
            }else{
                //data.field.qyID = $("#qyID").val();
                data.field.ksTime = start_time;
                data.field.jsTime = end_time;
                data.field.qyID = qyID;
                $.ajax({
                    url:HOST+"qiyeShoufei/addQiyeShoufei"
                    ,type:"POST"
                    ,contentType: 'application/json'
                    ,data:JSON.stringify(data.field)
                    ,success:function (data) {
                        if(data.code==0){
                            layer.close(layer.index);
                            layer.msg('缴费成功');
                            setTimeout(function(){
                                window.location.reload();
                            },1000);
                        }else if(data.code==1){
                            layer.msg('缴费失败');
                        }else{
                            layer.msg("拒绝访问");
                        }
                    }
                    ,error:function () {
                        console.log("ajax error");
                    }
                });
                return false;
            }
        });

        //遍历企业下选框
        $.ajax({
            url:HOST+"qiye/selectqiyes" ,
            success:function(res){
                if(res.code == 200) {
                    for(var i in res.data) {
                        $("#qiye").append("<option value='"+res.data[i].id+"'>"+res.data[i].qyname+"</option>");
                    }
                    form.render();
                }
            }
        })

        //遍历物业下选框
        $.ajax({
            url:HOST+"wuye/selectwuyestatus" ,
            success:function(res){
                if(res.code == 0) {
                    for(var i in res.data) {
                        $("#wuye").append("<option value='"+res.data[i].id+"'>"+res.data[i].wyname+"</option>");
                    }
                    form.render();
                }
            }
        })

        //物业企业联动
        form.on('select(wuye)', function(data){
            wuye = data.value;
            $.ajax({
                url:HOST+"qiye/selectqiyes" ,
                data:{wuye:wuye} ,
                success:function(res){
                    if(res.code == 200) {
                        $("#qiye option:gt(0)").remove();
                        for(var i in res.data) {
                            $("#qiye").append("<option value='"+res.data[i].id+"'>"+res.data[i].qyname+"</option>");
                        }
                        form.render();
                    }
                }
            })
        });

        //选中企业查询到期时间
        form.on('select(qiye)', function(data){
            var qiye = data.value;
            if(qiye==null || qiye==""){
                $("#lalala").css('display','none');
                $("#start_time").val("");
                $("#qyID").val("");
            }else{
                $.ajax({
                    url:HOST+"qiye/selectQiyeID" ,
                    data:{id:qiye} ,
                    success:function(res){
                        if(res.code == 200) {
                            $("#lalala").css('display','block');
                            $("#qyDqtime").val(res.data.qyDqtime);
                            $("#start_time").val(res.data.qyDqtime);
                            $("#qyID").val(res.data.id);
                        }
                    }
                })
            }
        });
    });
</script>
<script>
    layui.use('laydate', function(){
        var laydate = layui.laydate;
        var endDate= laydate.render({
            elem: '#end_time',//选择器结束时间
            type: 'datetime',
            min:"1970-1-1",//设置min默认最小值
            done: function(value,date){
                startDate.config.max={
                    year:date.year,
                    month:date.month-1,//关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                }
            }
        });
        //日期范围
        var startDate=laydate.render({
            elem: '#start_time',
            type: 'datetime',
            max:"2099-12-31",//设置一个默认最大值
            done: function(value, date){
                endDate.config.min ={
                    year:date.year,
                    month:date.month-1, //关键
                    date: date.date,
                    hours: 0,
                    minutes: 0,
                    seconds : 0
                };
            }
        });
    });
</script>
</body>
</html>